//
//  PicAddListsViewController.m
//  EducationAdminClient
//
//  Created by kwsdzjx on 16/7/11.
//  Copyright © 2016年 周佳兴. All rights reserved.
//

#import "PicAddListsViewController.h"
#import "PickTheAddBtnViewController.h"

@interface PicAddListsViewController ()<UIAlertViewDelegate>
//@property(strong, nonatomic)UIButton            *subBtn;
@property(nonatomic,retain)UIAlertView           *alertSure;
@property(nonatomic,retain)NSMutableArray        *showArr;
@property(nonatomic,retain)NSIndexPath           *indexPath;
@property(nonatomic,assign)BOOL                  needAdd;


@end

@implementation PicAddListsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    
    _showArr = [[NSMutableArray alloc]init];
    _sourceArr = [[NSMutableArray alloc]init];
    
    [_sourceArr addObjectsFromArray:_defaultSourceArr];
    
    _sendMutDic = [[NSMutableDictionary alloc]init];
    _mangManArr = [[NSMutableArray alloc]init];
    _sendsToMangManArr = [[NSMutableArray alloc]init];
    
    _needAdd = YES;
    //*** createUI
    [self createUI];
    
    
    [self createRightNavBar];

    [self createLeftNavBar];
}



-(void)createRightNavBar{
    
    
    
    UIButton *rightNavBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    rightNavBtn.frame = CGRectMake(0, 0, 50, 23);
    [rightNavBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    //    [rightNavBtn setBackgroundImage:[UIImage imageNamed:@"图层-6"] forState:UIControlStateNormal];
    [rightNavBtn addTarget:self action:@selector(rightClick:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *item = [[UIBarButtonItem alloc]initWithCustomView:rightNavBtn];
    //    self.navigationItem.leftBarButtonItem = item;
    [rightNavBtn setTitle:@"提交" forState:UIControlStateNormal];
    self.subBtn = rightNavBtn;
    
//    self.subBtn.hidden = YES;
    
    
    UIButton *rightNavBtn1 = [UIButton buttonWithType:UIButtonTypeCustom];
    rightNavBtn1.frame = CGRectMake(0, 0, 50, 23);
    [rightNavBtn1 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    //    [rightNavBtn setBackgroundImage:[UIImage imageNamed:@"图层-6"] forState:UIControlStateNormal];
    [rightNavBtn1 addTarget:self action:@selector(rightClick1:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *item1 = [[UIBarButtonItem alloc]initWithCustomView:rightNavBtn1];
    //    self.navigationItem.leftBarButtonItem = item;
    [rightNavBtn1 setTitle:@"添加" forState:UIControlStateNormal];

    self.navigationItem.rightBarButtonItems = @[item1, item];
    
}


-(void)rightClick:(UIButton *)btn{
    btn.enabled = NO;
    
    
    
        _alertSure =[[UIAlertView alloc]initWithTitle:@"提示" message:@"是否添加" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
        [_alertSure show];
    //    _alertSure =[[UIAlertView alloc]initWithTitle:@"提示" message:@"是否添加" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    //    [_alertSure show];
    
}

-(void)rightClick1:(UIButton *)btn{
//    btn.enabled = NO;
    
    PickTheAddBtnViewController *picAddVC = [[PickTheAddBtnViewController alloc]init];
    picAddVC.tableId = [NSString stringWithFormat:@"%@", _tableId];
    picAddVC.pageId = [NSString stringWithFormat:@"%@", _pageId];
    picAddVC.fromDataArr = _fromDataArr;
    picAddVC.fromMangManArr = _fromMangManArr;
    picAddVC.fromSendMutDic = _fromSendMutDic;
    picAddVC.delegateView = self;
    picAddVC.buttonDic = _buttonDic;
    picAddVC.tNum = _tNum;
    NSLog(@"%@", picAddVC.fromDataArr);
    _needAdd = YES;
    [self.navigationController pushViewController:picAddVC animated:YES];

    
//    _alertSure =[[UIAlertView alloc]initWithTitle:@"提示" message:@"是否添加" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
//    [_alertSure show];
//    //    _alertSure =[[UIAlertView alloc]initWithTitle:@"提示" message:@"是否添加" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
//    //    [_alertSure show];
    
}



-(void)receiveAddDataList:(NSDictionary *)dic{

    
//    NSDictionary *receiveSendDic = [dic valueForKey:@"sendD"];
//    NSArray *receiveMangMan = [dic valueForKey:@"mangManD"];
    if(_needAdd){
    
        [_sourceArr addObject:dic];
    }else{
        [_sourceArr replaceObjectAtIndex:_indexPath.row withObject:dic];
    }
//    [_sendsToMangManArr addObject:receiveSendDic];
//    [_sendMutDic setValuesForKeysWithDictionary:receiveSendDic];
//    [_mangManArr addObjectsFromArray:receiveMangMan];
    [self.tbView reloadData];

}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (alertView == _alertSure) {
        if(buttonIndex==1){
            
            [self addRequest];
        }
        else{
            _alertSure.hidden = YES;
        }
        
        self.subBtn.enabled = YES;
        return;
    }

}

#pragma mark - ADDReq
-(void)addRequest{
    
    
    NSMutableDictionary *dicSendBack = [[NSMutableDictionary alloc]init];
    
    
    
    for (NSDictionary *dic in _sourceArr) {
        

    NSDictionary *receiveSendDic = [dic valueForKey:@"sendD"];
    NSArray *receiveMangMan = [dic valueForKey:@"mangManD"];
    
    [_sendsToMangManArr addObject:receiveSendDic];
    [_sendMutDic setValuesForKeysWithDictionary:receiveSendDic];
    [_mangManArr addObjectsFromArray:receiveMangMan];
        
        
#warning maybe breaking out
        
        NSDictionary *dicA = [dic valueForKey:@"selectedDic"];
        
        NSArray *arrqqqqq = [dicA allKeys];
        arrqqqqq = [arrqqqqq sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2){
            NSComparisonResult result = [obj1 compare:obj2];
            return result==NSOrderedDescending;
        }];
        
        if(arrqqqqq.count>0){
            
//            cell.textLabel.text = [NSString stringWithFormat:@"%@", [dic valueForKey:arrqqqqq[0]]];
//            if(_showArr.count>indexPath.row+1){
//                [_showArr replaceObjectAtIndex:indexPath.row withObject:cell.textLabel.text];
//            }else{
                [_showArr addObject:[NSString stringWithFormat:@"%@", [dicA valueForKey:arrqqqqq[0]]]];
//            }
        }

    }
    
    
    
    [dicSendBack setValue:_sendMutDic forKey:@"sendD"];
    [dicSendBack setValue:_mangManArr forKey:@"mangManD"];
    [dicSendBack setValue:_sendsToMangManArr forKey:@"sends"];
    [dicSendBack setValue:_showArr forKey:@"shows"];
    [dicSendBack setValue:_sourceArr forKey:@"source"];

    if(self.delegateView){
        
        [self.delegateView performSelector:@selector(receiveAddData:) withObject:dicSendBack];
        
    }
    
    [self.navigationController popViewControllerAnimated:YES];
    
}

-(void)createUI{
    self.tbView  = [[UITableView alloc]init];
    [self.view addSubview:self.tbView];
    self.tbView.delegate = self;
    self.tbView.dataSource = self;
    
    [self.tbView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view.mas_top);
        make.left.equalTo(self.view.mas_left);
        make.bottom.equalTo(self.view.mas_bottom);
        make.right.equalTo(self.view.mas_right);
    }];
}

#pragma mark - UITableViewDataSource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.sourceArr.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *ID = @"PicAddCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
    
    
    NSDictionary *showDic = _sourceArr[indexPath.row];
#warning maybe breaking out
    
    NSDictionary *dic = [showDic valueForKey:@"selectedDic"];

    NSArray *arrqqqqq = [dic allKeys];
    arrqqqqq = [arrqqqqq sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2){
        NSComparisonResult result = [obj1 compare:obj2];
        return result==NSOrderedDescending;
    }];

    if(arrqqqqq.count>0){
        
        cell.textLabel.text = [NSString stringWithFormat:@"%@", [dic valueForKey:arrqqqqq[0]]];
//        if(_showArr.count>indexPath.row+1){
//            [_showArr replaceObjectAtIndex:indexPath.row withObject:cell.textLabel.text];
//        }else{
//            [_showArr addObject:cell.textLabel.text];
//        }
    }
    return cell;
}

#pragma mark - UITableViewDelegate
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    _indexPath = indexPath;
    PickTheAddBtnViewController *picAddVC = [[PickTheAddBtnViewController alloc]init];
    picAddVC.tableId = [NSString stringWithFormat:@"%@", _tableId];
    picAddVC.pageId = [NSString stringWithFormat:@"%@", _pageId];
    picAddVC.fromDataArr = _fromDataArr;
    picAddVC.fromMangManArr = _fromMangManArr;
    picAddVC.fromSendMutDic = _fromSendMutDic;
    picAddVC.defaultShowArr = _sourceArr[indexPath.row];
    picAddVC.delegateView = self;
    picAddVC.buttonDic = _buttonDic;
    picAddVC.tNum = _tNum;
    NSLog(@"%@", picAddVC.fromDataArr);
    _needAdd = NO;
    [self.navigationController pushViewController:picAddVC animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
